//*************************************************************************************
/** \file  avr_adc.cpp
 *    FIXME: This file needs a comment which describes what's in here. 
 *
 *  Revisions:
 *    \li  00-00-00  The Big Bang occurred, followed by the invention of waffles
 *
 *  License
 *    This file copyright 2007-2012 by somebody. It is released under the Lesser GNU
 *    public license, version 2. It is intended for educational use only, but the
 *    user is free to use it for any purpose permissible under the LGPL. The author
 *    has no control over the use of this file and cannot take any responsibility
 *    for any consequences of this use. 
 */
//*************************************************************************************

#include <stdlib.h>							// Include standard library header files
#include <avr/io.h>							// You'll need this for SFR and bit names

#include "rs232int.h"						// Include header for serial port class
#include "avr_adc.h"						// Include header for the A/D class


//-------------------------------------------------------------------------------------
/** This constructor sets up an A/D converter. You'll need to write a comment which 
 *  describes how it does that once you've written the constructor. 
 *  @param p_serial_port A pointer to the serial port which writes debugging info. 
 */

avr_adc::avr_adc (base_text_serial* p_serial_port)
{
	ptr_to_serial = p_serial_port;          // Store the serial port pointer locally

	// Note that when the serial port pointer is used to send a message, it must be
	// dereferenced with a "*", meaning "what is pointed to by ptr_to_serial" 
	*ptr_to_serial << "Setting up AVR A/D converter" << endl;

	// Initialize the run counter here. Nearly all class member variables need to be
	// initialized in the constructor
	runs = 0;

	// Code which actually sets something up should go here
	// TODO:  Well...? 
}


//-------------------------------------------------------------------------------------
/** This method takes one A/D reading from the given channel, and...?
 *  \param  channel The A/D channel which is being read must be from 0 to 7
 *  \return The result of the A/D conversion, or 0xFFFF if there was a timeout
 */

uint16_t avr_adc::read_once (uint8_t channel)
{
	// For now, just send a message saying this method is running
	*ptr_to_serial << "Running avr_adc::read_once()" << endl;

	// Increment the runs counter
	runs++;

	// Nothing has been put in here, so return an error until this method works
	return (0xFFFF);
}


//-------------------------------------------------------------------------------------
// FIXME: There should be a function which reads several channels into an array


//-------------------------------------------------------------------------------------
/** This operator prints an A/D converter on a text serial device. You may decide what
 *  is most useful to print; it should be some information about how the A/D converter
 *  is set up, what it's doing, current readings, or something like that. 
 */

base_text_serial& operator<< (base_text_serial& serial, avr_adc& my_adc)
{
	// You should replace this with something more meaningful; for now, it's just a
	// demonstration of how to print things.  It's recommended to remove the stuff
	// about counting the number of runs entirely: who cares how many times it's run?
	serial << "A/D converter: has run " << my_adc.get_runs () << " times, or 0x" 
	       << hex << my_adc.get_runs () << " times in hex." << dec << endl;

	// This statement should be left here; you must return a reference to the serial
	// device so that the "<<" operator can be used again and again on the same line
	return (serial);
}
